/* Copyright (c) 2024 Huawei Technologies Co., Ltd.
openFuyao is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN 'AS IS' BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */
import useLocalStorage from '@/hooks/useLocalStorage';
import { message, Popover } from 'antd';
import { StockOutlined } from '@ant-design/icons';
import DountChart from '@/components/DountChart';
import LineChart from '@/components/LineChart';
import { solveColorMap, solveEchartsClean } from '@/tools/utils';
import { mockData } from '@/mock/mock';
import { ResponseCode } from '@/common/constants';
import { useEffect, useState, useCallback } from 'openinula';
import { getColocationNodeCount, getColocationCluster } from '@/api/colocationApi';

let setTimeoutIdCluster = 0;
export default function ClusterMonitor({ propsType, propsColor }) {
  const [currentTheme, setCurrentTheme] = useLocalStorage('theme', 'light');
  const [messageApi, contextHolder] = message.useMessage();
  const [statisticsData, setStatisticsData] = useState();

  const getStaticData = useCallback(async () => {
    let finallyData = {};
    const nodeRes = await getColocationNodeCount();
    if (nodeRes.status === ResponseCode.OK) {
      finallyData.nodeInfo = [
        {
          name: '已开启混部功能节点数',
          value: nodeRes.data.data.colocationNodeCount,
        },
        {
          name: '未开启混部功能节点数',
          value: nodeRes.data.data.nonColocationNodeCount,
        },
      ];
      finallyData.nodeTotal = nodeRes.data.data.totalNodeCount;
      finallyData.deploymentTotal = nodeRes.data.data.podCount.normalCount +
        nodeRes.data.data.podCount.hlsCount +
        nodeRes.data.data.podCount.lsCount +
        nodeRes.data.data.podCount.beCount;
      finallyData.deploymentInfo = [
        {
          name: '非混部Pod数量',
          value: nodeRes.data.data.podCount.normalCount,
        },
        {
          name: 'HLS级别Pod数量',
          value: nodeRes.data.data.podCount.hlsCount,
        },
        {
          name: 'LS级别Pod数量',
          value: nodeRes.data.data.podCount.lsCount,
        },
        {
          name: 'BE级别Pod数量',
          value: nodeRes.data.data.podCount.beCount,
        },
      ];
    }

    const clusterRes = await getColocationCluster();
    if (clusterRes.status === ResponseCode.OK) {
      const { cpuUse, memoryUse, cpuApply, memoryApply, normalCpu, normalMemory } = solveEchartsClean(clusterRes.data.data);
      finallyData = { ...finallyData, cpuUse, memoryUse, cpuApply, memoryApply, normalCpu, normalMemory };
    }
    setStatisticsData(finallyData);
  }, []);

  const timeSearch = () => {
    localStorage.setItem('colocationTime1Id', setTimeoutIdCluster);
    clearTimeout(setTimeoutIdCluster);
    getStaticData();
    const clusterId = setTimeout(() => {
      timeSearch();
    }, 60000);
    setTimeoutIdCluster = clusterId;
  };

  useEffect(() => {
    if (setTimeoutIdCluster) {
      clearTimeout(setTimeoutIdCluster);
    }
    timeSearch();
    return () => {
      clearTimeout(setTimeoutIdCluster);
    };
  }, [currentTheme]);

  return <div className="monitor_namespace">
    <div style={{ background: currentTheme === 'dark' ? '#2a2d34ff' : '#fff', color: currentTheme === 'dark' ? '#fff' : '#333' }}>
      {contextHolder}
    </div>
    <div className='monitor_header'>
      <div className='monitor_dount'>
        <p className='title'>混部节点信息</p>
        <DountChart title="混部节点信息" id="node" data={statisticsData?.nodeInfo} total={{ text: '总节点数', value: statisticsData?.nodeTotal }} colorList={propsColor} />
      </div>
      <div className='monitor_dount'>
        <p className='title'>混部负载信息</p>
        <DountChart title="混部负载信息" id="deployment" data={statisticsData?.deploymentInfo} total={{ text: '总Pod数量', value: statisticsData?.deploymentTotal }} colorList={propsColor} />
      </div>
    </div>
    <div className='monitor_line_container'>
      <div className='each_monitor_container'>
        <p className='title'>集群CPU资源实际使用量（Core）</p>
        <LineChart id="cpuUse" data={statisticsData?.cpuUse} colorList={propsColor} isShowLegend={true} />
      </div>
      <div className='each_monitor_container'>
        <p className='title'>集群内存资源实际使用量（MiB）</p>
        <LineChart id="memoryUse" data={statisticsData?.memoryUse} colorList={propsColor} isShowLegend={true} />
      </div>
      <div className='each_monitor_container'>
        <p className='title'>集群超卖CPU申请量（Core）</p>
        <LineChart id="cpuApply" data={statisticsData?.cpuApply} colorList={propsColor} isShowLegend={true} />
      </div>
      <div className='each_monitor_container'>
        <p className='title'>集群超卖内存申请量（MiB）</p>
        <LineChart id="memoryApply" data={statisticsData?.memoryApply} colorList={propsColor} isShowLegend={true} />
      </div>
      <div className='each_monitor_container'>
        <p className='title'>集群通用CPU申请量（Core）</p>
        <LineChart id="normalCpu" data={statisticsData?.normalCpu} colorList={propsColor} isShowLegend={true} />
      </div>
      <div className='each_monitor_container'>
        <p className='title'>集群通用内存申请量（MiB）</p>
        <LineChart id="normalMemory" data={statisticsData?.normalMemory} colorList={propsColor} isShowLegend={true} />
      </div>
    </div>
  </div>;
}